Ubuntu 16.04上源码编译和安装pytorch教程,并编写C++ Demo CMakeLists.txt

您所在的位置:网站首页 libtorch download Ubuntu 16.04上源码编译和安装pytorch教程,并编写C++ Demo CMakeLists.txt

Ubuntu 16.04上源码编译和安装pytorch教程,并编写C++ Demo CMakeLists.txt

2023-02-15 19:42| 来源: 网络整理| 查看: 265

本文首发于个人博客https://kezunlin.me/post/54e7a3d8/,欢迎阅读最新内容!

tutorial to compile and use pytorch on ubuntu 16.04

PyTorch for Python install pytorch from anaconda conda info --envs conda activate py35 # newest version # 1.1.0 pytorch/0.3.0 torchvision conda install pytorch torchvision cudatoolkit=9.0 -c pytorch # old version [NOT] # 0.4.1 pytorch/0.2.1 torchvision conda install pytorch=0.4.1 cuda90 -c pytorch

output

The following NEW packages will be INSTALLED: pytorch pytorch/linux-64::pytorch-1.1.0-py3.5_cuda9.0.176_cudnn7.5.1_0 torchvision pytorch/linux-64::torchvision-0.3.0-py35_cu9.0.176_1

download from channel pytorch will cost much time! 下载pytorch/linux-64::pytorch-1.1.0-py3.5_cuda9.0.176_cudnn7.5.1_0速度非常慢!

install pytorch from tsinghua add tsinghua pytorch channels conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ # for legacy win-64 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/ conda config --set show_channel_urls yes

使用anaconda官方pytorch源非常慢,用清华源代替。 see tsinghua anaconda

cat ~/.condarc

channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ - defaults install pytorch from tsinghua conda create --name torch python==3.7 conda activate torch conda install -y pytorch torchvision conda install -y scikit-learn scikit-image pandas matplotlib pillow opencv

The following NEW packages will be INSTALLED:

pytorch anaconda/cloud/pytorch/linux-64::pytorch-1.1.0-py3.5_cuda9.0.176_cudnn7.5.1_0 torchvision anaconda/cloud/pytorch/linux-64::torchvision-0.3.0-py35_cu9.0.176_1 test pytorch import torch print(torch.__version__) '1.1.0'

or

python -c 'import torch; print(torch.cuda.is_available())' True pre-trained models

pre-trained model saved to /home/kezunlin/.cache/torch/checkpoints/

Downloading: "https://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth" to /home/kezunlin/.cache/torch/checkpoints/shufflenetv2_x0.5-f707e7126e.pth

PyTorch for C++ download LibTorch

download from LibTorch

compile from source compile pytorch # method 1 git clone --recursive https://github.com/pytorch/pytorch cd pytorch # method 2, if you are updating an existing checkout git clone https://github.com/pytorch/pytorch cd pytorch git submodule sync git submodule update --init --recursive

check tags

git tag -l v0.4.0 v0.4.1 v1.0.0 v1.0.1 v1.0rc0 v1.0rc1 v1.1.0

now compile

git checkout v1.1.0 # method 1: offical build will generate lots of errors #python setup.py install # method 2: normal make mkdir build && cd build && cmake-gui ..

with configs

BUILD_PYTHON OFF

be sure to use stable version 1.1.0 from here instead of latest version 20190724 (unstable version 1.2.0) because error will occurs when load models.

for 1.1.0:

std::shared_ptr module = torch::jit::load("./model.pt");

for latest 1.2.0

torch::jit::script::Module module = torch::jit::load("./model.pt");

configure output

******** Summary ******** General: CMake version : 3.5.1 CMake command : /usr/bin/cmake System : Linux C++ compiler : /usr/bin/c++ C++ compiler id : GNU C++ compiler version : 5.4.0 BLAS : MKL CXX flags : -fvisibility-inlines-hidden -fopenmp -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math Build type : Release Compile definitions : ONNX_ML=1;ONNX_NAMESPACE=onnx_torch;USE_GCC_ATOMICS=1;HAVE_MMAP=1;_FILE_OFFSET_BITS=64;HAVE_SHM_OPEN=1;HAVE_SHM_UNLINK=1;HAVE_MALLOC_USABLE_SIZE=1 CMAKE_PREFIX_PATH : CMAKE_INSTALL_PREFIX : /usr/local TORCH_VERSION : 1.1.0 CAFFE2_VERSION : 1.1.0 BUILD_CAFFE2_MOBILE : ON BUILD_ATEN_ONLY : OFF BUILD_BINARY : OFF BUILD_CUSTOM_PROTOBUF : ON Link local protobuf : ON BUILD_DOCS : OFF BUILD_PYTHON : OFF BUILD_CAFFE2_OPS : ON BUILD_SHARED_LIBS : ON BUILD_TEST : OFF INTERN_BUILD_MOBILE : USE_ASAN : OFF USE_CUDA : ON CUDA static link : OFF USE_CUDNN : ON CUDA version : 9.2 cuDNN version : 7.1.4 CUDA root directory : /usr/local/cuda CUDA library : /usr/local/cuda/lib64/stubs/libcuda.so cudart library : /usr/local/cuda/lib64/libcudart.so cublas library : /usr/local/cuda/lib64/libcublas.so cufft library : /usr/local/cuda/lib64/libcufft.so curand library : /usr/local/cuda/lib64/libcurand.so cuDNN library : /usr/local/cuda/lib64/libcudnn.so nvrtc : /usr/local/cuda/lib64/libnvrtc.so CUDA include path : /usr/local/cuda/include NVCC executable : /usr/local/cuda/bin/nvcc CUDA host compiler : /usr/bin/cc USE_TENSORRT : OFF USE_ROCM : OFF USE_EIGEN_FOR_BLAS : ON USE_FBGEMM : OFF USE_FFMPEG : OFF USE_GFLAGS : OFF USE_GLOG : OFF USE_LEVELDB : OFF USE_LITE_PROTO : OFF USE_LMDB : OFF USE_METAL : OFF USE_MKL : OFF USE_MKLDNN : OFF USE_NCCL : ON USE_SYSTEM_NCCL : OFF USE_NNPACK : ON USE_NUMPY : ON USE_OBSERVERS : ON USE_OPENCL : OFF USE_OPENCV : OFF USE_OPENMP : ON USE_TBB : OFF USE_PROF : OFF USE_QNNPACK : ON USE_REDIS : OFF USE_ROCKSDB : OFF USE_ZMQ : OFF USE_DISTRIBUTED : ON USE_MPI : ON USE_GLOO : ON USE_GLOO_IBVERBS : OFF NAMEDTENSOR_ENABLED : OFF Public Dependencies : Threads::Threads Private Dependencies : qnnpack;nnpack;cpuinfo;/usr/lib/x86_64-linux-gnu/libnuma.so;fp16;/usr/lib/openmpi/lib/libmpi_cxx.so;/usr/lib/openmpi/lib/libmpi.so;gloo;aten_op_header_gen;foxi_loader;rt;gcc_s;gcc;dl Configuring done install pytorch

now compile and install

make -j8 sudo make install

output

Install the project... -- Install configuration: "Release" -- Old export file "/usr/local/share/cmake/Caffe2/Caffe2Targets.cmake" will be replaced. Removing files [/usr/local/share/cmake/Caffe2/Caffe2Targets-release.cmake]. -- Set runtime path of "/usr/local/bin/protoc" to "$ORIGIN" -- Old export file "/usr/local/share/cmake/Gloo/GlooTargets.cmake" will be replaced. Removing files [/usr/local/share/cmake/Gloo/GlooTargets-release.cmake]. -- Set runtime path of "/usr/local/lib/libonnxifi_dummy.so" to "$ORIGIN" -- Set runtime path of "/usr/local/lib/libonnxifi.so" to "$ORIGIN" -- Set runtime path of "/usr/local/lib/libfoxi_dummy.so" to "$ORIGIN" -- Set runtime path of "/usr/local/lib/libfoxi.so" to "$ORIGIN" -- Set runtime path of "/usr/local/lib/libc10.so" to "$ORIGIN" -- Set runtime path of "/usr/local/lib/libc10_cuda.so" to "$ORIGIN:/usr/local/cuda/lib64" -- Set runtime path of "/usr/local/lib/libthnvrtc.so" to "$ORIGIN:/usr/local/cuda/lib64/stubs:/usr/local/cuda/lib64" -- Set runtime path of "/usr/local/lib/libtorch.so" to "$ORIGIN:/usr/local/cuda/lib64:/usr/lib/openmpi/lib" -- Set runtime path of "/usr/local/lib/libcaffe2_detectron_ops_gpu.so" to "$ORIGIN:/usr/local/cuda/lib64" -- Set runtime path of "/usr/local/lib/libcaffe2_observers.so" to "$ORIGIN:/usr/local/cuda/lib64"

pytorch 1.1.0 compile and install will cost more than 2 hours lib install to /usr/local/lib/libtorch.so cmake install to /usr/local/share/cmake/Torch

C++ example

load pytorch model in c++ see load pytorch model in c++

cpp #include // One-stop header. #include #include int main(int argc, const char* argv[]) { if (argc != 2) { std::cerr


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3